<?php
	require_once('config.php');
	require_once('db.php');
	require_once('charts/includes/FusionCharts.php');
	require_once('charts/includes/FC_Colors.php');
	define('BY_DAY', 0);
	define('BY_MONTH', 1);
	define('BY_YEAR', 2);
	if(isset($_POST['submit']) || isset($_POST['day_detail_submit']) || isset($_POST['hour_detail_submit'])){
		// get all post information
		$site_info = explode(":", $_POST['site']);
		$site_id = $site_info[0];
		$site_hostname = $site_info[1];
		$report_type = $_POST['type'];
		$year = $_POST['year'];
		$month = $_POST['month']+1;
		$day = $_POST['day'];

		$hour = -1; 	// default value of hour is 0
		if(isset($_POST['hour'])){
			$hour = $_POST['hour'];
		}

		$date = "";
		switch ($report_type){
			case BY_DAY:
					$date .= "$day/$month/$year";
				break;
			case BY_MONTH:
					$date .= "$month/$year";
					// get summary post value to see whether this report is a summary report.
					if(!isset($_POST['day_detail_submit'])){
						$day = -1;
					}
				break;
			case BY_YEAR:
					$date .= "$year";
				break;
			default:
				break;
		}

		$message = "<div align=\"center\" style=\"width: 80%;\"><h2>"
				. "Detail report of $site_hostname. Date: $date</h2>";

		// build the form for viewing by hour in the case of viewing report by day
		if($report_type == BY_DAY){
			$message .= "<form action=\"show_reports.php\" method=\"post\">";
			$message .= "<h3>Hour : "
					. "<select id=\"hour\" name=\"hour\">";
			for($i = -1; $i<=23; $i++){
				if($hour == $i){
					$message .= "<option value=\"$i\" selected=\"selected\">" . (($i > -1)?("Hour " . $i):"Summary") . "</option>";
				}else{
					$message .= "<option value=\"$i\">" . (($i > -1)?("Hour " . $i):"Summary") . "</option>";
				}
			}
			$message .= "</select>";
			$message .= "<input type=\"hidden\" name=\"year\" value=\"$year\" />";
			$message .= "<input type=\"hidden\" name=\"month\" value=\"" . ($month - 1) . "\" />";
			$message .= "<input type=\"hidden\" name=\"day\" value=\"$day\" />";
			$message .= "<input type=\"hidden\" name=\"site\" value=\"" . $_POST['site'] . "\" />";
			$message .= "<input type=\"hidden\" name=\"type\" value=\"$report_type\" />";
			$message .= "<input type=\"submit\" name=\"hour_detail_submit\" value=\"Show\" />";
			$message .= "</form></h3>";
			$message .= "</div>";
		}
		// build the form for viewing by day in the case of viewing report by month
		if($report_type == BY_MONTH){

			$message .= "<form action=\"show_reports.php\" method=\"post\">";
			$message .= "<h3>Day : "
					. "<select id=\"day\" name=\"day\">"
					. "<option value=\"-1\">Summary</option>";
			$days_in_month = date("t", mktime($month, null, $year));
			for($i = 1; $i<=$days_in_month; $i++){
				if($day == $i){
					$message .= "<option value=\"$i\" selected=\"selected\">" . ("Day " . $i) . "</option>";
				}else{
					$message .= "<option value=\"$i\">Day $i</option>";
				}
			}
			$message .= "</select>";
			$message .= "<input type=\"hidden\" name=\"year\" value=\"$year\" />";
			$message .= "<input type=\"hidden\" name=\"month\" value=\"" . ($month - 1) . "\" />";
			$message .= "<input type=\"hidden\" name=\"site\" value=\"" . $_POST['site'] . "\" />";
			$message .= "<input type=\"hidden\" name=\"type\" value=\"$report_type\" />";
			$message .= "<input type=\"submit\" name=\"day_detail_submit\" value=\"Show\" />";
			$message .= "</form></h3>";
			$message .= "</div>";
		}
		echo $message;
		echo "<div align=\"center\" style=\"width: 80%;\"><a href=\"reports.php\">Back to report selection page</a></div>";
		$result = array();
		$link = connectdb($servername, $dbname, $dbusername, $dbpassword);
		if(!$link){
			echo "<div align=\"center\" width=\"100%\"><h3>Can not connect to the database<h3></div>";
			exit;
		}
		$error = '';
		// show reports based on the selected creterias.
		switch($report_type){
			case BY_DAY:{
					// get all protocols
					$protocols = get_protocols($site_id, $link, $error);
					if($protocols === false){ // failed to get protocol
						echo "<div align=\"center\" width=\"100%\"><h3>$error</h3></div>";
						exit;
					}else{
						if(count($protocols) < 1){ // there is no protocol for this site
							echo "<div align=\"center\" width=\"100%\"><h3>There has no protocol configured for this site</h3></div>";
							exit;
						}
						foreach($protocols as $protocol){ // draw one chart for each protocol
							$error = '';
							if($hour >= 0){ // if $hour = -1, get summary records by days
								$records = get_checking_records_by_minute($site_id, $protocol['protocol_id'], $year, $month, $day, $hour, $link, $error);
							}else{ // if $hour >= 0, get summary records by minutes
								$records = get_checking_records_by_hour($site_id, $protocol['protocol_id'], $year, $month, $day, $link, $error);
							}
							if($records === false){
								echo "<div align=\"center\" width=\"100%\"><h3>$error</h3></div>";
								exit;
							}else{
								if(count($records) < 1){ // there is no record for this site and the protocol
									echo "<div align=\"center\" width=\"100%\"><h3>There has no checking record for protocol: " . $protocol['protocol_name'] . "</h3></div>";
									exit;
								}else{
									// import fusion chart javascript
									$minValue = 0;
									$maxValue = 0;
									echo "<SCRIPT LANGUAGE=\"Javascript\" SRC=\"charts/js/FusionCharts.js\"></SCRIPT>";
									if($hour >= 0){ // draw chart for hour detail
										// draw chart now
										$chart_title = "Monitoring result for protocol " . $protocol['protocol_name']
												. "(" . $protocol['checking_rate'] . " minutes ). Scale: 1:" . $chart_valueScale;
										$sub_title = "Hour detail";
										$strXML = "<graph caption='" . $chart_title
												. "' formatNumberScale='" . $chart_formatNumberScale
												. "' decimalPrecision='" . $chart_decimalPrecision
												. "' rotateNames='" . $chart_rotateNames
												. "' yAxisName= 'Response Time(second)"
												. "' xAxisName= 'Start time"
												. "' baseFontSize='" . $chart_baseFontSize
												. "' yAxisMinValue='%s"
												. "'>";
												;
										$minValue = $records[0]['operating_time'];
										foreach($records as $record){
	      									$strXML .= "<set name='" . substr($record['starting_time'], 11) . "' value='" . ($record['operating_time']* $chart_valueScale) . "' colo='" . getFCColor() . "' />";
	      									//find the minimum total time to identify the yAxisMinValue
	      									if($record['operating_time'] < $minValue){
	      										$minValue = $record['operating_time'];
	      									}
	      									//find the maximum total time to identify the yAxisMaxValue
											if($record['operating_time'] > $maxValue){
	      										$maxValue = $record['operating_time'];
	      									}
										}
										$strXML = sprintf($strXML, ($minValue/2) * $chart_valueScale);
										$strXML .= "</graph>";
									}else{ // draw chart for hour summary
										$chart_title = "Monitoring result for protocol " . $protocol['protocol_name'] . "(" . $protocol['checking_rate'] . " minutes ). Scale: 1:" . $chart_valueScale;
										$sub_title = "Hour summary";
										$strXML = "<graph caption='" . $chart_title
												. "' subcaption='" . $sub_title
												. "' formatNumberScale='" . $chart_formatNumberScale
												. "' decimalPrecision='" . $chart_decimalPrecision
												. "' rotateNames='" . $chart_rotateNames
												. "' yAxisName= 'Total up time(second)"
												. "' xAxisName= 'Hour"
												. "' baseFontSize='" . $chart_baseFontSize
												. "' yAxisMinValue='%s"
												. "'>";

										$minValue = $records[0]['total_time'];
										foreach($records as $record){
	      									$strXML .= "<set name='" . $record['hour'] . "' value='" . ($record['total_time']* $chart_valueScale) . "' colo='" . getFCColor() . "' />";
	      									//find the minimum total time to identify the yAxisMinValue
											if($record['total_time'] < $minValue){
	      										$minValue = $record['total_time'];
	      									}
	      									//find the maximum total time to identify the yAxisMaxValue
											if($record['total_time'] > $maxValue){
	      										$maxValue = $record['total_time'];
	      									}
										}
										$strXML = sprintf($strXML, ($minValue/2) * $chart_valueScale);
										$strXML .= "</graph>";
									}

								}
								$chartWidth = ($chart_baseWidth * count($records) > $chart_minWidth)?($chart_baseWidth * count($records)) : $chart_minWidth;
								$chartHeight = ($chart_baseHeight * 30  > $chart_minHeight)?($chart_baseHeight * 30) : $chart_minHeight;
								echo renderChart("charts/swf/FCF_Line.swf", "", $strXML, $protocol['protocol_name'] . "_checkingRecords", $chartWidth, $chartHeight);
							}
						}
					}
				}
				break;
			case BY_MONTH:
				{
					// get all protocols
					$protocols = get_protocols($site_id, $link, $error);
					if($protocols === false){ // failed to get protocol
						echo "<div align=\"center\" width=\"100%\"><h3>$error</h3></div>";
						exit;
					}else{
						if(count($protocols) < 1){ // there is no protocol for this site
							echo "<div align=\"center\" width=\"100%\"><h3>There has no protocol configured for this site</h3></div>";
							exit;
						}
						foreach($protocols as $protocol){ // draw one chart for each protocol
							$error = '';
							if($day >= 0){ // if $day = -1, get summary records by days
								$records = get_checking_records_by_hour($site_id, $protocol['protocol_id'], $year, $month, $day, $link, $error);
							}else{ // if $day >= 0, get summary records by minutes
								$records = get_checking_records_by_day($site_id, $protocol['protocol_id'], $year, $month, $link, $error);
							}
							if($records === false){
								echo "<div align=\"center\" width=\"100%\"><h3>$error</h3></div>";
								exit;
							}else{
								if(count($records) < 1){ // there is no record for this site and the protocol
									echo "<div align=\"center\" width=\"100%\"><h3>There has no checking record for protocol: " . $protocol['protocol_name'] . "</h3></div>";
									exit;
								}else{
									// import fusion chart javascript
									$minValue = 0;
									$maxValue = 0;
									echo "<SCRIPT LANGUAGE=\"Javascript\" SRC=\"charts/js/FusionCharts.js\"></SCRIPT>";
									if($day >= 0){ // draw chart for day detail
										// chart title
										$chart_title = "Monitoring result for protocol " . $protocol['protocol_name']
												. "(" . $protocol['checking_rate'] . " minutes ). Scale: 1:" . $chart_valueScale;
										// char subtitle
										$sub_title = "Day detail";
										$strXML = "<graph caption='" . $chart_title
												. "' formatNumberScale='" . $chart_formatNumberScale
												. "' decimalPrecision='" . $chart_decimalPrecision
												. "' rotateNames='" . $chart_rotateNames
												. "' yAxisName= 'Total up time(second)"
												. "' xAxisName= 'Hour"
												. "' baseFontSize='" . $chart_baseFontSize
												. "' yAxisMinValue='%s"
												. "'>";
												;
										$minValue = $records[0]['total_time'];
										foreach($records as $record){
	      									$strXML .= "<set name='" . $record['hour'] . "' value='" . ($record['total_time']* $chart_valueScale) . "' colo='" . getFCColor() . "' />";
	      									//find the minimum total time to identify the yAxisMinValue
	      									if($record['total_time'] < $minValue){
	      										$minValue = $record['total_time'];
	      									}
	      									//find the maximum total time to identify the yAxisMaxValue
											if($record['total_time'] > $maxValue){
	      										$maxValue = $record['total_time'];
	      									}
										}
										$strXML = sprintf($strXML, ($minValue/2) * $chart_valueScale);
										$strXML .= "</graph>";
									}else{	// draw chart for day summary
										//title
										$chart_title = "Monitoring result for protocol " . $protocol['protocol_name'] . "(" . $protocol['checking_rate'] . " minutes ). Scale: 1:" . $chart_valueScale;
										$sub_title = "Day summary";
										$strXML = "<graph caption='" . $chart_title
												. "' subcaption='" . $sub_title
												. "' formatNumberScale='" . $chart_formatNumberScale
												. "' decimalPrecision='" . $chart_decimalPrecision
												. "' rotateNames='" . $chart_rotateNames
												. "' yAxisName= 'Total up time(second)"
												. "' xAxisName= 'Day"
												. "' baseFontSize='" . $chart_baseFontSize
												. "' yAxisMinValue='%s"
												. "'>";

										$minValue = $records[0]['total_time'];
										foreach($records as $record){
	      									$strXML .= "<set name='" . $record['day'] . "' value='" . ($record['total_time']* $chart_valueScale) . "' colo='" . getFCColor() . "' />";
	      									//find the minimum total time to identify the yAxisMinValue
											if($record['total_time'] < $minValue){
	      										$minValue = $record['total_time'];
	      									}
	      									//find the maximum total time to identify the yAxisMaxValue
											if($record['total_time'] > $maxValue){
	      										$maxValue = $record['total_time'];
	      									}
										}
										$strXML = sprintf($strXML, ($minValue/2) * $chart_valueScale);
										$strXML .= "</graph>";
									}

								}
								$chartWidth = ($chart_baseWidth * count($records) > $chart_minWidth)?($chart_baseWidth * count($records)) : $chart_minWidth;
								$chartHeight = ($chart_baseHeight * 30  > $chart_minHeight)?($chart_baseHeight * 30) : $chart_minHeight;
								echo renderChart("charts/swf/FCF_Line.swf", "", $strXML, $protocol['protocol_name'] . "_checkingRecords", $chartWidth, $chartHeight);
							}
						}
					}
				}
				break;
			case BY_YEAR:
				break;
			default:
				break;
		}

	}
?>
